package com.jhx.common.utils;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;

import com.jhx.common.app.CommonModule;

/**
 * 作者: Ryan
 * 创建时间:15/1/27 16:07
 * 类描述:用户信息保存和获取工具类
 */
public class SystemConfigUtils {

    private static SystemConfigUtils systemConfigUtils;
    private static String versionName = "";

    private SystemConfigUtils() {
    }

    public static String getVersionName() {
        if (!TextUtils.isEmpty(versionName)) {
            return versionName;
        }
        try {
            PackageInfo pInfo = CommonModule.Companion.getApplication().getPackageManager()
                    .getPackageInfo(CommonModule.Companion.getApplication().getPackageName(), 0);
            versionName = pInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static String getVersionStr() {
        StringBuffer sb = new StringBuffer();
        String versionName = getVersionName();
        sb.append(versionName).append(".0.");
        sb.append(getChannel());
        return sb.toString();
    }

    /**
     * 获取渠道号
     */
    public static String getChannel() {
//        return getMetaData("UMENG_CHANNEL", Integer.class);
//        return String.valueOf(BuildConfig.channel);
        return "";
    }

    public static int getVersionCode() {
        PackageInfo pInfo = null;
        try {
            pInfo = CommonModule.Companion.getApplication().getPackageManager()
                    .getPackageInfo(CommonModule.Companion.getApplication().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (null != pInfo) {
            return pInfo.versionCode;
        }
        return 0;
    }

    public static SystemConfigUtils getSystemConfigUtils() {
        if (systemConfigUtils == null) {
            synchronized (SystemConfigUtils.class) {
                systemConfigUtils = new SystemConfigUtils();
            }
        }
        return systemConfigUtils;
    }

    /**
     * 获取Metadata的值
     *
     * @param key          metadata的值
     * @param <T>获取的值的数据类型
     * @return metadata的值
     */
    public static <T> T getMetaData(String key, Class<T> c) {
        try {
            ApplicationInfo ai = CommonModule.Companion.getApplication().getPackageManager().
                    getApplicationInfo(CommonModule.Companion.getApplication().getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            T value = null;
            if (null != bundle) {
                Object object = bundle.get(key);
                if (null != object) {
                    value = (T) object;
                }
            }
            return value;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取String资源
     *
     * @param resID
     * @return
     */
    public static int getColorRes(int resID) {
        int color = CommonModule.Companion.getApplication().getResources().getColor(resID);
        return color;
    }

}